﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.Text;


public partial class design_page_admin_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_menber_add = new DATA_operate();   //创建数据库操作类对象
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            TextBox_id.Enabled = false;
            
        }
        if (DropDownList_usertype.SelectedItem.Text=="学生")
        {
            DropDownList_class.Enabled = true;
        }
        else
            DropDownList_class.Enabled = false;    //学生才需要班级这项。其他的都禁掉


        TextBox_pwd1.Text = "111111";
        TextBox_pwd2.Text = "111111";
        TextBox_id.Text = Make_memberid(); 
    }



    private string Make_memberid()   //生成成员ID
    {
        string numA="";
        if (DropDownList_usertype.SelectedItem.Text == "学生")
        {
            numA = data_operate_in_menber_add.dataread("MAX(_Sid)", "_student", true);   //获得数据库最大成员ID
            if (numA==null)
            {
                numA = "2123012010001";
            }
        }
        else if (DropDownList_usertype.SelectedItem.Text == "老师")
        {
            numA = data_operate_in_menber_add.dataread("MAX(_Tid)", "_teacher", true);   //获得数据库最大成员ID
            if (numA == null)
            {
                numA = "1123012010001";
            }
        }
        else if (DropDownList_usertype.SelectedItem.Text == "管理员")
        {
            numA = data_operate_in_menber_add.dataread("MAX(_Aid)", "_admin", true);   //获得数据库最大成员ID
            if (numA == null)
            {
                numA = "0123012010001";
            }
        }
      
        string numB = "1";   //最大ID 号加1

        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }





    protected void Button2_Click(object sender, EventArgs e)   //添加数据
    {
        Label7.Visible = true;
        if(TextBox_pwd1.Text==""||TextBox_pwd2.Text=="")
        {
            Label7.Text = "*添加失败：密码不能为空";
        }
        else if(TextBox_pwd1.Text!=TextBox_pwd2.Text)
        {
            Label7.Text = "*添加失败：两次输入的密码不一致";
        }
        else if(TextBox_name.Text=="")
        {
            Label7.Text = "*添加失败：姓名不能为空";
        }
        else
        {
            string userid = TextBox_id.Text;
            string userpwd = TextBox_pwd2.Text;
            string username = TextBox_name.Text;
            string usersex = DropDownList_sex.SelectedItem.Text;
            string userphone = TextBox_phone.Text;

            switch (DropDownList_usertype.SelectedItem.Text)
            {
                case "学生":
                    string userclass = DropDownList_class.SelectedItem.Text;
                    string classid = data_operate_in_menber_add.dataread("_class", "_Cid", "_Cname", userclass);
                    string add_sql_student = userid + "," + userpwd + "," + username + "," + usersex + "," + userphone + "," + classid +   "," + "0";

                    if (data_operate_in_menber_add.dataadd("_student", add_sql_student))
                    {
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加学生成功');", true);
                        Label7.Text = "*添加学生成功----" + "ID:" + userid + "  姓名:" + username;                
                    }
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加失败');", true);
                    break;
                case "老师":
                    string add_sql_teacher = userid + "," + userpwd + "," + username + "," + usersex + "," + userphone + "," + "0";
                    if (data_operate_in_menber_add.dataadd("_teacher", add_sql_teacher))
                    {
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加学生成功');", true);
                        Label7.Text = "*添加老师成功----" + "ID:" + userid + "  姓名:" + username;
               
                
                    }
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加失败');", true);
                    break;
                case "管理员":
                    string add_sql_admin = userid + "," + userpwd + "," + username + "," + usersex + "," + userphone + "," + "0";
                    if (data_operate_in_menber_add.dataadd("_admin", add_sql_admin))
                    {
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加学生成功');", true);
                        Label7.Text = "*添加管理员成功----" + "ID:" + userid + "  姓名:" + username;
                     
              
                    }
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('添加失败');", true);
                    break;

            }
            Button3_Click(sender, e);
           
        }
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox_id.Text =Make_memberid();
        TextBox_pwd1.Text = "111111";
        TextBox_name.Text = "";
        TextBox_pwd2.Text = "111111";
        TextBox_phone.Text = "";
    }
}